package xyz.renyumeng.xkm.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import xyz.renyumeng.xkm.model.user.UserAddressInfo;
import xyz.renyumeng.xkm.model.user.UserInfo;
import xyz.renyumeng.xkm.result.Result;
import xyz.renyumeng.xkm.user.dto.UserInfoDto;
import xyz.renyumeng.xkm.user.dto.UserLoginDto;
import xyz.renyumeng.xkm.user.dto.UserLoginParam;
import xyz.renyumeng.xkm.user.service.UserAddressInfoService;
import xyz.renyumeng.xkm.user.service.UserLoginService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author renyumeng
 * @date Created in 14:56 2023/7/5
 */

@RestController
@Api(tags = "用户个人信息接口")
@RequestMapping("/customer/user")
public class UserController {
    @Resource
    private UserLoginService userLoginService;

    @Resource
    private UserAddressInfoService userAddressInfoService;


    @GetMapping("/test")
    @ApiOperation(value = "测试接口")
    public Result<String> test() {
        return Result.ok("成功!");
    }

    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginDto> login(@RequestBody UserLoginParam userLoginParam) {
        return userLoginService.login(userLoginParam);
    }

    @GetMapping("/logout")
    @ApiOperation(value = "用户退出登录")
    public Result<?> logout(@RequestParam(value = "userId", required = false) Long userId,
                            @RequestParam(value = "satoken", required = false) String satoken) {
        return userLoginService.logout(userId, satoken);
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result<?> register(@RequestBody UserLoginParam userLoginParam) {
        return userLoginService.register(userLoginParam);
    }

    @PutMapping("/edit")
    @ApiOperation("编辑用户个人信息")
    public Result<?> editUserInfo(@RequestBody UserInfoDto userInfoDto) {
        return userLoginService.editUserInfo(userInfoDto);
    }

    @GetMapping("/{userId}")
    @ApiOperation("根据用户id获取用户信息")
    public Result<UserInfo> getUserInfoById(@PathVariable Long userId) {
        return userLoginService.getUserInfoByUserId(userId);
    }

    @GetMapping("/address/all/{parentId}")
    @ApiOperation("查询所有地址的子节点")
    public Result<List<UserAddressInfo>> getAddressInfoByParentId(@PathVariable Long parentId){
        return userAddressInfoService.getAddressInfoByParentId(parentId);
    }

    @GetMapping("/address/{addressId}")
    @ApiOperation("查询用户的地址的全称")
    public Result<UserAddressInfo> getUserAddressInfoByUserId(@PathVariable Long addressId){
        return userAddressInfoService.getUserAddressInfoByUserId(addressId);
    }


}
